<?php
/**
 * ActivityController.class.php
 * 主题活动控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-13
 */

namespace Wechat\Controller;

class ActivityController extends BaseController {

	function index(){
		$userInfo 	= C('User');
		// //自动执行签到
		// D('Check')->checkToday($userInfo['id']);

		$level 	= C('user.child_level') ? C('user.child_level') : 1;
		$info['activity_info']	= D('Activity')->getNewItem($level);
		$info['activity_list']	= D('Activity')->getList($level, 1);
		$info['leavemsg_list']	= D('LeaveMsg')->getList(2, $info['activity_info']['id'], 1); 
		$info['join_count']	= get_online_count();
		$info['check_info']	= D('Check')->getLastInfo($userInfo['id']);

		$this->response($info);
	}

	function getList(){
		$level 	= C('user.child_level') ? C('user.child_level') : 1;
		$page 	= $this->request('p', 1);
		$list 	= D('Activity')->getList($level, $page);
		$this->outOInfo([
			'list' => $list,
			'user_info'	=> C('user')
		]);
	}

	function getItem(){
		$this->unEmptyParam(['activity_id']);

		$activityId 	= $this->iInfo['activity_id'];
		$activityInfo 	= D('Activity')->getItem($activityId);
		if($activityInfo === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$leaveMsgList 	= D('LeaveMsg')->getList(D('Activity')::LEAVE_MSG_TYPE, $activityId, 2);
		if($leaveMsgList === false)
			$this->outOINfo(D('LeaveMsg')->errMsg, 0);

		$pictureList 	= D('Activity')->getPictureList(C('user.id'), $activityId);
		if($pictureList === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$this->outOInfo([
			'activity' => $activityInfo, 
			'leavemsg_list' => $leaveMsgList,
			'picture_list' => $pictureList,
			'user_info'	=> C('user')
		]);
	}

	/**
	 * 获取最新活动信息
	 * @return 	$info
	 */
	function getNewItem(){
		$userInfo 	= C('User');
		$childLevel 	= $userInfo['child_level'] ? $userInfo['child_level'] : 1;
		$activityInfo 	= D('Activity')->getNewItem($childLevel);
		if($activityInfo === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$leaveMsgList 	= D('LeaveMsg')->getList(D('Activity')::LEAVE_MSG_TYPE, $activityInfo['id'], 2);
		if($leaveMsgList === false)
			$this->response(D('LeaveMsg')->errMsg, 0);

		$pictureList 	= D('Activity')->getPictureList(C('user.id'), $activityInfo['id']);
		if($pictureList === false)
			$this->response(D('Activity')->errMsg, 0);

		$this->response([
			'activity' => $activityInfo, 
			'leavemsg_list' => $leaveMsgList,
			'picture_list' => $pictureList,
			'user_info'	=> $userInfo
		]);
	}

	/**
	 * 上传活动照片
	 * @param 	$activity_id
	 * @param 	$child_pics 	宝宝拍摄
	 * @param 	$parent_pics 	父母拍摄
	 * @return 	boolean
	 */
	function addPicture(){
		$this->unEmptyParam(['activity_id', 'child_pics', 'parent_pics']);

		$activityId 	= $this->request('activity_id');
		$childPics 		= $this->request('child_pics');
		$parentPics 	= $this->request('parent_pics');

		$Image 	= new \Wechat\Util\Image();
		//文件路径整合
		foreach($childPics as &$val){
			$item['path']	= strchr($val, '/server');
			$item['beautify_path']	= $Image->beautifyPicture(rand(1, 6), strchr($val, 'Uploads'));
			$val 	= $item;
		}
		foreach($parentPics as &$val){
			$item['path']	= strchr($val, '/server');
			$item['beautify_path']	= $Image->beautifyPicture(rand(1, 6), strchr($val, 'Uploads'));
			$val 	= $item;
		}
		$result 	= D('Activity')->addPicture($activityId, C('user.id'), $childPics, $parentPics);
		if($result === false)
			$this->outOInfo(D('Activity')->errMsg, 0);
		$this->outOInfo();
	}

	/**
	 * 获取主题活动照片
	 * @param 	$activity_id
	 * @return 	$picture_list
	 */
	function getPictureList(){
		$this->unEmptyParam(['activity_id']);

		$activityId 	= $this->request('activity_id');
		$userId 	= C('user.id');

		$list 	= D('Activity')->getPictureList($userId, $activityId);
		if($list === false)
			$this->outOInfo(D('Activity')->errMsg, 0);
		$this->outOInfo(['list' => $list]);
	}
}